﻿//浏览器类型
var SysAgent = {};
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject)
    SysAgent.ie = ua.match(/msie ([\d.]+)/)[1]
else if (document.getBoxObjectFor)
    SysAgent.firefox = ua.match(/firefox\/([\d.]+)/)[1]
else if (window.MessageEvent && !document.getBoxObjectFor)
    SysAgent.chrome = ua.match(/chrome\/([\d.]+)/)[1]
else if (window.opera)
    SysAgent.opera = ua.match(/opera\/([\d.]+)/)[1]
else if (window.openDatabase)
    SysAgent.safari = ua.match(/version\/([\d.]+)/)[1];
    
var moveX = 0;
var moveY = 0;
var moveTop = 0;
var moveLeft = 0;
var moveable = false;
var docMouseMoveEvent = document.onmousemove;
var docMouseUpEvent = document.onmouseup;
addDivonmousedown = function() {
    //alert('dd');
    msgObj = this;
    var evt = getEvent();
    moveable = true;
    moveX = evt.clientX;
    moveY = evt.clientY;
    moveTop = parseInt(msgObj.style.top);
    moveLeft = parseInt(msgObj.style.left);
    document.onmousemove = function() {
        if (moveable) {
            var evt = getEvent();
            var x = moveLeft + evt.clientX - moveX;
            var y = moveTop + evt.clientY - moveY;
            if (x > 0 && (x + w < iWidth) && y > 0 && (y + h < iHeight)) {
                msgObj.style.left = x + "px";
                msgObj.style.top = y + "px";
            }
        }
    };
    document.onmouseup = function() {
        if (moveable) {
            document.onmousemove = docMouseMoveEvent;
            document.onmouseup = docMouseUpEvent;
            moveable = false;
            moveX = 0;
            moveY = 0;
            moveTop = 0;
            moveLeft = 0;
        }
    };
}

function getEvent() {
    return window.event || arguments.callee.caller.arguments[0];
}


var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_mouseposX;
var popup_mouseposY;
var popup_oldfunction;
function popup_display(x) {
    var win = window.open();
    for (var i in x) win.document.write(i + ' = ' + x[i] + '<br>');
}
function popup_mousedown(e) {
    var ie = navigator.appName == "Microsoft Internet Explorer";
    if (ie && window.event.button != 1) return;
    if (!ie && e.button != 0) return;
    popup_dragging = true;
    popup_target = this['target'];
    popup_mouseX = ie ? window.event.clientX : e.clientX;
    popup_mouseY = ie ? window.event.clientY : e.clientY;
    if (ie)
        popup_oldfunction = document.onselectstart;
    else popup_oldfunction = document.onmousedown;
    if (ie)
        document.onselectstart = new Function("return false;");
    else document.onmousedown = new Function("return false;");
}
function popup_mousemove(e) {
    if (!popup_dragging) return;
    popup_target = drag_element['target'];
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    var mouseX = ie ? window.event.clientX : e.clientX;
    var mouseY = ie ? window.event.clientY : e.clientY;
    element.style.left = (element.offsetLeft + mouseX - popup_mouseX) + 'px';
    element.style.top = (element.offsetTop + mouseY - popup_mouseY) + 'px';
    popup_mouseX = ie ? window.event.clientX : e.clientX;
    popup_mouseY = ie ? window.event.clientY : e.clientY;
}

function popup_mouseup(e) {
    if (!popup_dragging) return;
    popup_dragging = false;
    popup_target = drag_element['target'];
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    if (ie)
        document.onselectstart = popup_oldfunction;
    else document.onmousedown = popup_oldfunction;
}
function popup_exit(e) {
    popup_target = drag_element['target'];
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    popup_mouseup(e);
    element.style.display = "none"; 
}
function popup_show(popupId, dragId, exitId, top, left, ismask, maskid, bodyalpha,notallowdrag) {
    element = document.getElementById(popupId);
    drag_element = document.getElementById(dragId);
    exit_element = document.getElementById(exitId);
    element.style.position = "absolute";     
    element.style.left = (left == null) ? ((document.documentElement.scrollLeft + popup_mouseposX - 10) + 'px') : left;
    element.style.top = (top == null) ? ((document.documentElement.scrollTop + popup_mouseposY - 10) + 'px') : top;
    drag_element['target'] = popupId;
    element.style.display = "block";  
    if (ismask != null && ismask == true && maskid && bodyalpha) {
         maskBody(bodyalpha, maskid);
         exit_element.onclick = function(e) { e = e || window.event; popup_exit(e); remaskBody(maskid); }
         if (notallowdrag != true)
            drag_element.onmousedown = function(e) { e = e || window.event; popup_mousedown(e); }
    }
    else {
        if (notallowdrag != true)
            drag_element.onmousedown = popup_mousedown;
        exit_element.onclick = popup_exit;
    }
    //drag_element.onmousedown = popup_mousedown;
    //exit_element.onclick = popup_exit;
}
function popup_mousepos(e) {
    var ie = navigator.appName == "Microsoft Internet Explorer";
    popup_mouseposX = ie ? window.event.clientX : e.clientX;
    popup_mouseposY = ie ? window.event.clientY : e.clientY;
}
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmousedown', popup_mousepos);
else document.addEventListener('mousedown', popup_mousepos, false);
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmousemove', popup_mousemove);
else document.addEventListener('mousemove', popup_mousemove, false);
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmouseup', popup_mouseup);
else document.addEventListener('mouseup', popup_mouseup, false);

//隐藏body,出现半透明
function maskBody(bodyalpha, maskid) {
    document.getElementById(maskid).className = 'maskStyle';
    document.getElementById(maskid).style.visibility = "visible";
    document.getElementById(maskid).style.width = document.body.scrollWidth + "px";
    document.getElementById(maskid).style.height = document.body.scrollHeight + "px";
    if (SysAgent.ie) {
        //ie
        document.getElementById(maskid).style.filter = "alpha(opacity = " + bodyalpha.toString() + ")";
    }
    if (SysAgent.firefox) {
        //ff
        document.getElementById(maskid).style.MozOpacity = bodyalpha / 100;
    } if (SysAgent.chrome) {
        //chrome
        document.getElementById(maskid).style.opacity = bodyalpha / 100;
    }
}
//显示
function remaskBody(maskid) {
    document.getElementById(maskid).style.visibility = "hidden";
    document.getElementById(maskid).style.width = "0px";
    document.getElementById(maskid).style.height = "0px";
    popup_target = drag_element['target'];
    document.getElementById(popup_target).style.diaply = "none";
}

////隐藏body,出现半透明
//function maskBody(bodyalpha, maskid) {
//    var maskeEle = $("#" + maskid)[0];
//    maskeEle.className = 'maskStyle';
//    maskeEle.style.visibility = "visible";
//    maskeEle.style.width = document.body.scrollWidth + "px";
//    maskeEle.style.height = document.body.scrollHeight + "px";
//    if (SysAgent.ie) {
//        //ie
//        maskeEle.style.filter = "alpha(opacity = " + bodyalpha.toString() + ")";
//    }
//    if (SysAgent.firefox) {
//        //ff
//        maskeEle.style.MozOpacity = bodyalpha / 100;
//    } if (SysAgent.chrome) {
//        //chrome
//        maskeEle.style.opacity = bodyalpha / 100;
//    }
//}
////显示
//function remaskBody(maskid) {
//    $("#" + maskid)[0].style.visibility = "hidden";
//    $("#" + drag_element['target'])[0].style.display = "none";
//    $("#" + maskid)[0].style.width = "0px";
//    $("#" + maskid)[0].style.height = "0px";
//}